package com.starlink.common.log.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.starlink.common.log.ApplicationLogContext;
import com.starlink.common.log.DefaultApplicationLogContext;
import com.starlink.common.log.DefaultUserInfoFunction;
import com.starlink.common.log.UserInfoFunction;
import com.starlink.common.log.aspect.CommonLogAspect;
import com.starlink.common.log.configuration.CommonLogConfiguration;
import com.starlink.common.log.configuration.CommonWebConfiguration;
import com.starlink.common.log.configuration.WebhookConfiguration;
import com.starlink.common.log.util.LarkMessageUtil;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * @author: lichao
 * @date: 2021-06-22 14:51:59
 */
@ConditionalOnWebApplication
@Configuration
@AutoConfigureAfter({GsonAutoConfiguration.class, JacksonAutoConfiguration.class, JsonbAutoConfiguration.class})
public class CommonLogConfig {

    @Bean
    @ConditionalOnMissingBean
    public ObjectMapper objectMapper() {
        return Jackson2ObjectMapperBuilder.json().build();
    }

    @Bean
    @ConditionalOnMissingBean(value = {UserInfoFunction.class})
    public UserInfoFunction userInfoFunction(){
        return new DefaultUserInfoFunction();
    }

    @Bean
    public ApplicationLogContext applicationLogContext(){
        return new DefaultApplicationLogContext();
    }


    @Bean
    @ConfigurationProperties(prefix ="starlink.webhook" )
    public WebhookConfiguration webhookConfig(){
        return new WebhookConfiguration();
    }
    @Bean
    @ConfigurationProperties(prefix ="starlink.common.log" )
    public CommonLogConfiguration commonLogConfiguration(){
        return new CommonLogConfiguration();
    }
    @Bean
    public CommonLogAspect  commonLogAspect(){
        return new CommonLogAspect();
    }

    @Bean
    public LarkMessageUtil larkMessageUtil(){
        return new LarkMessageUtil();
    }

    @Bean
    public CommonWebConfiguration commonWebConfiguration(){
        return new CommonWebConfiguration();
    }
}
